package com.gg.communication.ui.ctr;

import com.gg.communication.communication.BaseCommunication;
import com.gg.communication.communication.tcp.TcpServer;
import com.gg.communication.communication.udp.server.UdpServer;
import com.gg.communication.event.ReceiveListener;
import com.gg.communication.util.ByteUtil;
import javafx.event.ActionEvent;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import org.controlsfx.control.ToggleSwitch;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class ServerController {

    public TextField port;
    public Button start;
    public TextArea sendText;
    public Button send;
    public TextArea receiveText;
    public CheckBox isHex;
    public CheckBox revHex;
    public ToggleSwitch cement;
    public Button sendAll;
    public TextField ip;
    BaseCommunication server;

    public ServerController() {
    }

    /**
     * 启动服务
     *
     * @param actionEvent
     */
    public void start(ActionEvent actionEvent) {
        if (cement.isSelected()) {
            server = TcpServer.initServer();
        } else {
            server = UdpServer.initServer();

        }
        server.setPort(Integer.parseInt(port.getText()));
        try {
            server.setReceiveListener(new ReceiveListener() {
                @Override
                public void receive(byte[] message, BaseCommunication communication) {
                    System.out.println(ByteUtil.byteArrayToHexString(message));
                    //if (revHex.isSelected()) {
                    //    receiveText.appendText(ByteUtil.byteArrayToHexString(message) + "\n");
                    //} else {
                    //    receiveText.appendText(new String(message) + "\n");
                    //}
                }

                @Override
                public void err(Exception e) {

                }
            });
            server.start();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送消息
     *
     * @param actionEvent
     */
    public void sendAllMsg(ActionEvent actionEvent) throws ExecutionException, InterruptedException {
        byte[] msg = sendText.getText().getBytes();
        if (isHex.isSelected()) {
            msg = ByteUtil.hexStringToByteArray(sendText.getText().replaceAll(" ", ""));
        }
        byte[] message = server.send(msg);
        if (revHex.isSelected()) {
            receiveText.appendText(ByteUtil.byteArrayToHexString(message) + "\n");
        } else {
            receiveText.appendText(new String(message) + "\n");
        }
    }

    public void sendMsg(ActionEvent actionEvent) throws ExecutionException, InterruptedException, IOException {
        byte[] msg = sendText.getText().getBytes();
        if (isHex.isSelected()) {
            msg = ByteUtil.hexStringToByteArray(sendText.getText().replaceAll(" ", ""));
        }
        byte[] message = null;
        //if () {
        message = TcpServer.initInstance(ip.getText(), Integer.parseInt(port.getText())).send(msg);
        //}
        System.out.println("<==");
        System.out.println(ByteUtil.byteArrayToHexString(message));
        if (revHex.isSelected()) {
            receiveText.appendText(ByteUtil.byteArrayToHexString(message) + "\n");
        } else {
            receiveText.appendText(new String(message) + "\n");
        }
    }

    public void close(ActionEvent actionEvent) {
        try {
            if (server != null) {
                server.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
